"""
auth : ginger
    日志封装
    控制台打印
    日志文件写入和切割 ( 按 天 切割 , 保留 3 个日志文件 )
"""
import logging
import time
from logging.handlers import TimedRotatingFileHandler

from common.do_conf import log_level
from common.do_conf import console_level
from common.do_conf import file_level
from common.do_file_path import LOG_PATH


class DoLog(object):
    def __new__(cls, *args, **kwargs):
        my_first_logger = logging.getLogger('my_adv_logger')
        my_first_logger.setLevel(log_level)
        foromat = '%(asctime)s - %(levelname)s - ' \
                  'code:%(filename)s - line:%(lineno)d -->  ' \
                  '%(message)s'

        # 输出到控制台:
        sh = logging.StreamHandler()
        sh.setLevel(console_level)
        sh.setFormatter(logging.Formatter(foromat))
        my_first_logger.addHandler(sh)

        # 按时间切割：
        """
        LOG_TIME_PATH               :       路径
        when                        :       按照什么切割(一般是 d , 按照天进行切割)
        backupCount                 :       保留几份, 这里随意, 想保留几份就是几份.
        timefilehandler.suffix      :       后缀,这里是年月日,时分秒
        """
        time_file_handler = TimedRotatingFileHandler(LOG_PATH, when='d', backupCount=3, encoding='utf8')
        time_file_handler.suffix = "%Y-%m-%d_%H-%M-%S.log"
        time_file_handler.setFormatter(logging.Formatter(foromat))
        time_file_handler.setLevel(file_level)
        my_first_logger.addHandler(time_file_handler)

        return my_first_logger


do_log = DoLog()

if __name__ == '__main__':
    for i in range(5):
        time.sleep(2)
        do_log.debug('我是一条debug等级的日志xxxxxx')
        do_log.info('我是一条info等级的日志xxxxxxxx')
